package com.xtaller.easy.admin.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.xtaller.common.convert.J;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Taller on 2018/4/13
 */
public class R extends HashMap<String,Object> {

    public static ResponseEntity error(){
        return error(HttpStatus.INTERNAL_SERVER_ERROR,"server error");
    }
    public static ResponseEntity error(String message){
        return error(HttpStatus.BAD_REQUEST,message);
    }
    public static ResponseEntity error(HttpStatus status, String message){
        return error(status, message, new JSONObject(), 0);
    }
    public static ResponseEntity error(HttpStatus status, String message, int isRefresh){
        return error(status, message, new JSONObject(), isRefresh);
    }
    public static ResponseEntity error(HttpStatus status, String message, Object object, int isRefresh){
        R r = new R();
        r.put("message", message);
        r.put("result", J.formatObject(object));
        r.put("isRefresh", isRefresh);
        return new ResponseEntity<Map<String,Object>>(r,status);
    }

    public static ResponseEntity ok(String message){
        return ok(message,new JSONObject());
    }
    public static ResponseEntity ok(Object object){
        return ok("操作成功",object);
    }
    public static ResponseEntity ok(String message, Object object){
        R r = new R();
        r.put("message", message);
        r.put("result", J.formatObject(object));
        return new ResponseEntity<Map<String,Object>>(r, HttpStatus.OK);
    }

    public R put(String key,Object val){
        super.put(key,val);
        return this;
    }


}
